﻿//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//

/**
 *	base
 *      @author: CYS
 */


var canvas;
var g2D;

var height;
var width;
var fullWindow = true;
var ratio = 1;

var time = 0;
var dtime = 0;
var startime;
var lasttime;
var g_infoShow = new infoShow();
var bClear = false;
var clearTime = 0;

var heartPoints = [];
var numHeartPoints = 64;
var heartTrails;

var lineColor = 'rgba(255, 255, 225, 1)';

function canvasResize()
{
	ratio = window.innerWidth/canvas.width;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	width = canvas.width;
	height = canvas.height;
	if(!g2D)g2D = canvas.getContext("2d");

	heartShape(width/2, height/2);
}

function heartShape(mx, my)
{
	for(var i = 0; i < numHeartPoints;  i++)
	{
		var C = Math.cos;
		var s = Math.PI*2*i/numHeartPoints;
		var x = mx + height*0.32*Math.pow(Math.sin(s), 3);
		var y = my + height*0.32/18.0 * (-(15*C(s) - 5*C(2*s) - 2*C(3*s) - C(4*s)));
		heartPoints[i] = [x, y];
	}
}


function init()
{
	canvas = document.getElementById('canvas2D');
	canvasResize();

	var touchable = 'createTouch' in document;
	if (touchable)
	{
		canvas.addEventListener('touchstart', onTouchStart, false);
		canvas.addEventListener('touchmove', onTouchMove, false);
		canvas.addEventListener('touchend', onTouchEnd, false);
	}
	else
	{
		canvas.addEventListener('mousedown', onTouchStart, false);
		canvas.addEventListener('mousemove', onTouchMove, false);
		canvas.addEventListener('mouseup', onTouchEnd, false);
	}

	canvas.addEventListener( 'contextmenu', function ( event ) { event.preventDefault(); }, false );



	heartTrails = new trails();
	heartTrails.init(width, height, numHeartPoints);

}

var bTimeout = false;
window.onresize = function()
{
	if (fullWindow) canvasResize();
	bTimeout = true;
}

var first = true;
var lastOrien;

window.ondeviceorientation =  function(e)
{
	if(first)
	{
		lastOrien = window.orientation;
		first = false;
	}
	if(window.orientation !== lastOrien)
	{
		window.onresize();
		lastOrien = window.orientation;
	}

	g_infoShow.add("alpa: ", e.alpha);
	g_infoShow.add("beta: ",e.beta);
	g_infoShow.add("gamma: ",e.gamma);
	g_infoShow.add("orientation: ",window.orientation);
}

window.onblur = function()
{
	bTimeout = true;
}

function drawScene()
{
	//g2D.globalCompositeOperation = "lighter";
	//g2D.globalAlpha = 0.5;

	g2D.fillStyle = "rgba(0,0,0,0.2)"; // clear screen
	//g2D.fillStyle = "#000"; // clear screen
	g2D.fillRect(0,0,width,height);
	heartTrails.updata(heartPoints);

	// g_infoShow.show();

}



function onTouchStart(event)
{
	// Prevent the browser from doing its default thing (scroll, zoom)
	event.preventDefault();


	if(event.clientX) // on mouse down
	{
		heartShape(event.clientX, event.clientY);
	}

	if(!event.touches) return;
	// on touch star
	heartShape(event.touches[0].clientX, event.touches[0].clientY);

	for(var i = 0; i < event.touches.length; i++)
	{
	}

}

function onTouchMove(event)
{
	event.preventDefault();

	if(event.clientX) // on mouse move
	{
	}

	if(!event.touches) return;

	for(var i = 0; i < event.touches.length; i++)
	{

	}


}

function onTouchEnd(event)
{
	event.preventDefault();

	if(event.clientX) // on mouse up
	{
	}
	if(!event.touches) return;

	for(var i = 0; i < event.touches.length; i++)
	{


	}
	numBrushes = event.touches.length;

}

//setInterval(function()
//{
//	drawScene();
//}, 25);

function gameloop()
{
	window.requestAnimFrame( gameloop );
	if(bTimeout)
	{
		lasttime = Date.now();
		bTimeout = false;
	}
	dtime = Date.now() - lasttime;
	time = Date.now() - startime;
	drawScene();
	lasttime = Date.now();

}


function game()
{
	init();
	gameloop();
}




